<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Leshi
 *
 * @author dragon.hu add @2016.3.10
 */
require_once 'BaseChannel.php';

//乐视
class Leshi implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="90606b4fb23944109f41bd6939326fda"; //ScrKey
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("leshi", $_REQUEST);
		
		$app_id = $_REQUEST['app_id'];  //应用所属AppId
		$lepay_order_no = $_REQUEST['lepay_order_no'];  //乐视支付平台流水号
		$letv_user_id = $_REQUEST['letv_user_id'];  //乐视用户id
		$out_trade_no = $_REQUEST['out_trade_no'];  //支付sdk订单号
		$pay_time = $_REQUEST['pay_time'];  //支付sdk订单号
		$price = $_REQUEST['price'];  //支付金额
		$product_id = $_REQUEST['product_id'];  //商品id
		$sign = $_REQUEST['sign'];  //签名
		$sign_type = $_REQUEST['sign_type'];  //签名类型
		$trade_result = $_REQUEST['trade_result'];  //交易结果
		$version = $_REQUEST['version'];  //版本号
		$cooperator_order_no = $_REQUEST['cooperator_order_no'];  //cp订单号
		$extra_info = $_REQUEST['extra_info'];  //cp自定义信息
		$original_price = $_REQUEST['original_price'];  //商品原始价格
		
		$arr = $_REQUEST;
		ksort($arr);
		$mystr = "";
		foreach ($arr as $k => $v)
		{
			if ($k == 'sign' || $k == 'cooperator_order_no' || $k== 'extra_info' || $k == 'original_price')  continue;
			$mystr .= $k.'='.$v.'&';
		}
		$mystr .= 'key='.$paymentKey;
		$mysign = md5($mystr);
		
		if($sign == $mysign)  
		{ //验证通过
			 $tmpstatus = STATUS_SUCCESS;
			 if ($trade_result != 'TRADE_SUCCESS')
			 {
			 	$tmpstatus = STATUS_FAILD;
			 }
			//进行发货
			if(false == $orderLog->recordOrder($tmpstatus, $price, $cooperator_order_no, "leshi", $platform,$lepay_order_no))
			{
				$ret="fail";
				$orderLog->log("leshi",array("order"=>$cooperator_order_no,"money"=>$price,"serverresult"=>"faild"));
			}else 
			{
				//支付成功
				$ret="success";
				$orderLog->log("leshi",array("order"=>$cooperator_order_no,"money"=>$price,"serverresult"=>"success"));
			}
			 
		} else {
			//"$sig验证失败"
			$orderLog->log("leshi","  order:$cooperator_order_no  sign error sign:$sign mysign:$mysign");
			$ret="sign error";
		}
		return $ret;
	}
	
}